﻿#ifndef SHOWHOME_H
#define SHOWHOME_H

#include "header.h"
#include "database.h"
#include "loginWindow.h"
#include "user-ProductList.h"
#include "user-businessListPage.h"
#include "rider-orderPage.h"
#include "user-orderPage.h"
#include "All-profilePage.h"
#include "adminPage.h"
#include "admin_window.h"
#include "buss_main.h"

QT_BEGIN_NAMESPACE
namespace Ui { class ShowHome; }
QT_END_NAMESPACE

// 前向声明
class BusinessPage;

class ShowHome : public QMainWindow
{
    Q_OBJECT
public:
    explicit ShowHome(QWidget *parent = nullptr);
    ~ShowHome();

    void setupUI(bool islogin);
    void updateLoginStatus(bool loggedIn, const QString &username = QString());
    void getName();

private:
    Ui::ShowHome *ui;
    database db;
    QPushButton *right_close;
    QPushButton *right_login;
    QLineEdit *searchBox;
    bool isLogin = false; // 登录状态
    QString currentUser;  // 当前用户名
    int userIdentity = -1; // 用户身份
    int phone_id = -1;
    QString identityName;
    QString name;   // 查询对象
    QSqlQuery query;
    
    // 状态栏按钮
    QAction *homeAction;
    QAction *orderAction;
    QAction *adminAction;
    QAction *profileAction;
    
    // 页面组件
    QStackedWidget *stackedWidget;
    BusinessListPage *businessListPage;
    UserProductList *productList;
    OrderPage *orderPage;
    UserOrderPage *userOrderPage;
    AdminPage *adminPage;
    AdminWindow *adminWindow;
    ProfilePage *profilePage;

    Buss_main *bm = nullptr; // 商家后台窗口指针
    int currentBusinessId = -1; // 当前商家ID
    QString currentBusinessName; // 当前商家名称

private slots:
    void onLoginClicked();      // 登录/个人信息按钮点击
    void onLogoutClicked();     // 退出按钮点击
    void onBusinessSelected(int businessId, const QString &businessName); // 商户选择
    void onBackToHomeClicked(); // 返回首页按钮点击
    
    // 状态栏按钮点击
    void onHomeActionTriggered();
    void onOrderActionTriggered();
    void onAdminActionTriggered();
    void onProfileActionTriggered();
    
    // 辅助函数：更新状态栏按钮样式
    void updateStatusBarButtonStyles(int activeIndex);
};

// 商户商品页面类
class BusinessPage : public QWidget
{
    Q_OBJECT

public:
    BusinessPage(database &db, QWidget *parent = nullptr);
    void loadBusinessProducts(const QString &businessName, int businessId = -1);

private:
    QTableWidget *productsTable;
    database &db;
    QPushButton *backButton;
    QLabel *businessInfoLabel;

    
    // 购物车相关
    struct CartItem {
        int productId;
        QString productName;
        double price;
        int quantity;
    };
    QMap<int, CartItem> cart; // 购物车，键为商品ID
    double deliveryFee; // 配送费
    int businessId; // 当前商户ID
    
    // 底部统计区域
    QLabel *totalLabel; // 总金额标签
    QPushButton *checkoutButton; // 确认购买按钮
    
    // 更新购物车数量
    void updateQuantity(int productId, int row, int delta);
    // 更新总金额
    void updateTotalAmount();

signals:
    void backButtonClicked();
};

#endif // SHOWHOME_H
